//--------------------------------------------------------------------//
//     此代码由T4模板自动生成										  //
//	   生成时间 2020-11-12 09:12:30 	                              //
//	   描述：应用接入配置表
//--------------------------------------------------------------------//

using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Linq;
using Dapper;
using Newtonsoft.Json.Linq;
using RayD.Core;
using RayD.Repository;
using RayD.Repository.Entites.SystemModule;
using System;

namespace RayD.App.SystemModule
{
    public class AppConfigApp
    {
        #region 构造函数注入
        private IRepository BaseRepository;
        public AppConfigApp(IRepository repository)
        {
            this.BaseRepository = repository;
        }
        #endregion

        #region 获取数据
        /// <summary>
        /// 获取实体
        /// </summary>
        /// <param name="AppId">AppId</param>
        /// <returns></returns>
        [Caching]
        public virtual AppConfigEntity GetEntityByAppId(string AppId)
        {
            return this.BaseRepository.FindEntity<AppConfigEntity>(c =>
                   c.B_DeleteMark.Equals(DeleteMark.Undeleted)
                && c.B_EnabledMark.Equals(EnabledMark.Enabled)
                && c.B_AppId.Equals(AppId)
            );
        }
        /// <summary>
        /// 获取列表数据
        /// <summary>
        /// <param name=\"queryJson\">查询参数</param>
        /// <returns></returns>
        public IEnumerable<AppConfigEntity> GetList(string queryJson)
        {
            var expression = LinqExtensions.True<AppConfigEntity>();
            var queryParam = queryJson.ToJObject();
            //过滤 系统后台接入应用
            expression = expression.And(c =>
                c.B_DeleteMark.Equals(DeleteMark.Undeleted) &&
                c.B_Id != "17ddc7a8-80ab-4327-86a9-f9adf1a0d110"
            );
            return this.BaseRepository.IQueryable(expression);
        }
        #endregion

        #region 提交数据

        /// <summary>
        /// 保存实体数据（新增、修改）
        /// <summary>
        /// <param name="formEntity">数据实体</param>
        /// <returns></returns>
        [RemoveCache]
        public virtual (bool, string) SaveForm(AppConfigEntity formEntity)
        {
            if (!formEntity.B_Id.IsEmpty())
            {
                //编辑(Null、主键不会更改)
                formEntity.Modify();
                this.BaseRepository.Update(formEntity);
            }
            else
            {
                //新增
                formEntity.Create();
                this.BaseRepository.Insert(formEntity);
            }
            return (true, "保存成功！");
        }
        /// <summary>
        /// 删除实体数据
        /// <summary>
        /// <param name="formEntity">数据实体</param>
        /// <returns></returns>
        [RemoveCache]
        public virtual (bool, string) DeleteForm(AppConfigEntity formEntity)
        {
            var entity = this.BaseRepository.FindEntity<AppConfigEntity>(formEntity.B_Id);
            if (!entity.IsEmpty())
            {
                entity.Delete();
                this.BaseRepository.Update(entity);
                return (true, "删除成功！");
            }
            else
            {
                return (true, "删除失败！");
            }
        }
        /// <summary>
        /// 状态更改
        /// <summary>
        /// <param name="formEntity">数据实体</param>
        /// <returns></returns>
        [RemoveCache]
        public virtual (bool, string) ChangeState(AppConfigEntity formEntity)
        {
            if (!formEntity.B_Id.IsEmpty())
            {
                this.BaseRepository.Update(new AppConfigEntity() { B_Id = formEntity.B_Id, B_EnabledMark = formEntity.B_EnabledMark });
                return (true, "更改成功！");
            }
            else
            {
                return (false, "更改失败！");
            }
        }
        #endregion

        #region 数据验证
        /// <summary>
        /// 验证字段值是否存在
        /// </summary>
        /// <param name="id">实体主键</param>
        /// <param name="field">字段名</param>
        /// <param name="value">字段值</param>
        /// <returns></returns>
        public bool ValidateExist(string id, string field, string value)
        {
            var expression = LinqExtensions.True<AppConfigEntity>();
            if (!string.IsNullOrEmpty(id))
            {
                expression = expression.And(t => t.B_Id != id);
            }
            return this.BaseRepository.IQueryable(expression).Exist(field, value);
        }
        #endregion
    }
}

